package chapter9;

import javax.vecmath.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import java.applet.*;
import com.sun.j3d.utils.applet.MainFrame;
/**
 * windows
 * @author isource
 *
 */
public class Ex1 extends Applet {
  public static void main(String[] args) {
    new MainFrame(new Ex1(), 640, 640);
  }

  public void init() {
    // create canvas
    GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
    Canvas3D cv = new Canvas3D(gc);
    setLayout(new BorderLayout());
    add(cv, BorderLayout.CENTER);
    BranchGroup bg = createSceneGraph();
    bg.compile();
    SimpleUniverse su = new SimpleUniverse(cv);
    su.getViewingPlatform().setNominalViewingTransform();
    su.addBranchGraph(bg);
  }

  private BranchGroup createSceneGraph() {
    BranchGroup root = new BranchGroup();
    TransformGroup spin = new TransformGroup();
    spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    root.addChild(spin);
    // squares
    // vertex color specified in geometry 
    QuadArray geom = new QuadArray(4, GeometryArray.COORDINATES|GeometryArray.COLOR_3);
    Point3f[] coords = {new Point3f(-1,-1,0), new Point3f(1,-1,0), new Point3f(1,1,0), new Point3f(-1,1,0)};
    geom.setCoordinates(0, coords);
    Color3f[] colors = {new Color3f(1,0,0), new Color3f(0,1,0), new Color3f(0,0,1), new Color3f(1,1,1)};
    geom.setColors(0, colors);
    Shape3D shape1 = new Shape3D(geom);
    // lit object
    QuadArray geom2 = new QuadArray(4, GeometryArray.COORDINATES|GeometryArray.NORMALS);
    geom2.setCoordinates(0, coords);
    Vector3f normal = new Vector3f(0, 0, 1);
    for (int i = 0; i < 4; i++) geom2.setNormal(i, normal);
    Appearance appear = new Appearance();
    Material material = new Material();
    appear.setMaterial(material);
    Shape3D shape2 = new Shape3D(geom2, appear);
    // coloring attributes in appearance 
    Appearance appear3 = new Appearance();
    ColoringAttributes coloring = new ColoringAttributes( new Color3f(1f,0,0), ColoringAttributes.SHADE_FLAT);
    appear3.setColoringAttributes(coloring);
    Shape3D shape3 = new Shape3D(geom2, appear3);
    // no coloring specification
    Shape3D shape4 = new Shape3D(geom2);
    // object transformation
    Transform3D tr = new Transform3D();
    tr.setScale(0.25);
    tr.setTranslation(new Vector3f(-0.5f,0.5f,0f));
    TransformGroup tg1 = new TransformGroup(tr);
    spin.addChild(tg1);
    tg1.addChild(shape1);
    tr.setTranslation(new Vector3f(0.5f,0.5f,0f));
    TransformGroup tg2 = new TransformGroup(tr);
    spin.addChild(tg2);
    tg2.addChild(shape2);
    tr.setTranslation(new Vector3f(-0.5f,-0.5f,0f));
    TransformGroup tg3 = new TransformGroup(tr);
    spin.addChild(tg3);
    tg3.addChild(shape3);
    tr.setTranslation(new Vector3f(0.5f,-0.5f,0f));
    TransformGroup tg4 = new TransformGroup(tr);
    spin.addChild(tg4);
    tg4.addChild(shape4);
    //rotating
    Alpha alpha = new Alpha(-1, 8000);
    RotationInterpolator rotator = new RotationInterpolator(alpha, spin);
    BoundingSphere bounds = new BoundingSphere();
    rotator.setSchedulingBounds(bounds);
    spin.addChild(rotator);
    //background and light
    AmbientLight light = new AmbientLight(true, new Color3f(Color.red));
    light.setInfluencingBounds(bounds);
    root.addChild(light);
    PointLight ptlight = new PointLight(new Color3f(Color.green), 
      new Point3f(3f,3f,3f), new Point3f(1f,0f,0f));
    ptlight.setInfluencingBounds(bounds);
    root.addChild(ptlight);
    PointLight ptlight2 = new PointLight(new Color3f(Color.orange),
      new Point3f(-2f,2f,2f), new Point3f(1f,0f,0f));
    ptlight2.setInfluencingBounds(bounds);
    root.addChild(ptlight2);
    return root;
  }
}